(function($) {
$.fn.capitalize = function() {
	$(this).keypress(function(e) {
		if (window.event) { /* IE 7 */
			key = window.event.keyCode;
			if ((key > 0x60) && (key < 0x7B)) {
				window.event.keyCode = key-0x20;
			}
		} else if(!e.ctrlKey) { /* Firefox, IE8*/
			if (e.target.createTextRange) {
                var r = document.selection.createRange().duplicate();
                r.moveEnd('character', e.target.value.length);
                if (r.text == '') mstart = e.target.value.length;
                else mstart = e.target.value.lastIndexOf(r.text);
                r.moveStart('character', -e.target.value.length);
                mend = r.text.length;
	        } else {
	                mstart = e.target.selectionStart;
	                mend = e.target.selectionEnd;
	        }
	 
	        if(e.which>0x60 && e.which<0x7B) {
	                e.preventDefault();
	                //e.stopPropagation();
	                z = $(e.target).val();
	                front = z.substring(0, mstart);
	                back = z.substring(mend);
	                $(e.target).val(front+String.fromCharCode(e.which-0x20)+back);
	 
	                if(e.target.createTextRange) { 
	                    var range = e.target.createTextRange(); 
	                    range.move("character", mend+1); 
	                    range.select(); 
	                } else if(e.target.selectionStart) { 
	                    e.target.focus(); 
	                    e.target.setSelectionRange(mend+1, mend+1); 
	                }  
	        }
		}
    });
	$(this).blur(function(e) {
		$(this).val($(this).val().toUpperCase());
    });
};
})(jQuery);